<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of category
 *
 * @author QUYET
 */
class category {
    var $children_array = array();
    var $option_ar = array();
    var $value_ar = array();
    var $id_name_ar = array();
    var $option_store = array();
    var $rows = array();
    var $category_path = array();
    var $language = 1;//french
    /* private */
    private $name_prefix = "----";

    //put your code here
    public function __construct() {
        $this->CI = &get_instance();
        $this->CI->load->database();
        if($this->CI->session->userdata('lang')=='en') $this->language = 2;
    }

    function build_option_data($root_id, $name, $deep) {
        $filter = array('parent_id' => $root_id);
        if ($deep >= 0) {
            $this->option_ar[] = repeater($this->name_prefix, $deep - 1) . $name;
            $this->value_ar[] = $root_id;
            $this->id_name_ar[$root_id] = $name;
            $this->option_store[] = array('id' => $root_id, "name" => repeater($this->name_prefix, $deep) . $name);
            $row['display'] = repeater($this->name_prefix, $deep - 1) . $name;
            $row['value'] = $root_id;
            $row['deep'] = $deep;
            $this->rows[] = $row;
        }
        $list_item = $this->get_categories($filter);
        foreach ($list_item as $item) {
            $this->build_option_data($item["id"], $item["category"], $deep + 1);
        }
    }

    function get_all_categories($id_category) {
        $this->CI->db->select('ots_ticket_cat.id,ots_ticket_cat_lang.category,ots_ticket_cat.parent_id,ots_ticket_cat.depth');
        $this->CI->db->from('ots_ticket_cat');
        $this->CI->db->join('ticket_cat_lang','ots_ticket_cat.id=ticket_cat_lang.id_category');        
        $this->db->where('ticket_cat_lang.id_lang',$this->language);
        return $this->CI->db->get()->result_array();
    }
    
    function get_categories($filter) {
        $this->CI->db->select('ots_ticket_cat.id,ots_ticket_cat_lang.category,ots_ticket_cat.parent_id,ots_ticket_cat.depth');
        $this->CI->db->from('ots_ticket_cat');
        $this->CI->db->join('ticket_cat_lang','ots_ticket_cat.id=ticket_cat_lang.id_category');        
        $this->CI->db->where('ticket_cat_lang.id_lang',$this->language);
        $this->CI->db->where($filter);
        return $this->CI->db->get()->result_array();
    }
    
    function build_children_array($id_cat){
        $children = $this->get_children($id_cat);
        if(count($children)>0){
            foreach ($children as $child) {
                $this->build_children_array($child['id']);
            }
        } else{
            $this->children_array[] = $id_cat;
        }
    }
    
    function get_children($id_cat){
        //return $this->CI->db->get_where('ots_ticket_cat',array('parent_id'=>$id_cat))->result_array();
        
        $this->CI->db->select('ots_ticket_cat.id,ots_ticket_cat_lang.category,ots_ticket_cat.parent_id,ots_ticket_cat.depth');
        $this->CI->db->from('ots_ticket_cat');
        $this->CI->db->join('ticket_cat_lang','ots_ticket_cat.id=ticket_cat_lang.id_category');        
        $this->CI->db->where('ticket_cat_lang.id_lang',$this->language);
        $this->CI->db->where('parent_id',$id_cat);
        
        return $this->CI->db->get()->result_array();
    }

}

?>
